
<?php

if (!empty($_SESSION['login'])&&!empty($_SESSION['email'])){
$db = new Mypdo();
if(empty($_POST)){
    $locManager =new LocationManager($db);
    $listeLoc = $locManager->getList();

    ?>

    <h1>Crée locataire</h1>

    <form method="post" action="#">

        <p>

            <label for="id_loc">Location :</label><select name="id_loc" id="id_loc" required="required">
                <option></option>
                <?php foreach($listeLoc as $key=>$value){
                    ?><option <?php if(!empty($_GET['idl'])){echo $_GET['idl'] == $value->getIdLoc()?'selected':'';} ?> value="<?php echo $value->getIdLoc(); ?>"><?php echo $value->getNomLoc(); ?></option><?php echo "\n";
                } ?>


            </select>
        <h3>Identité</h3>
        <label for="genre_loca">Genre :</label><select name="genre_loca" id="genre_loca" required="required">
            <option></option>
            <option value="M">M.</option>
            <option value="Mme">Mme.</option>
            <option value="Mlle">Mlle.</option>
        </select>
        <label for="nom_loca">Nom:</label><input name="nom_loca" type="text" id="nom_loca" required="required" />
        <label for="prenom_loca">Prenom :</label><input type="text" name="prenom_loca" id="prenom_loca" required="required" /><br />
        
        
        
        <label for="date_bail_deb_loca">Date de début de bail :</label><input type="date" name="date_bail_deb_loca" id="date_bail_deb_loca"  required="required"  /><br />
        <label for="date_bail_fin_loca">Date de fin de bail :</label><input type="date" name="date_bail_fin_loca" id="date_bail_fin_loca" required="required"  /><br />

        <label for="date_bail_rev_loca">Date de revision de bail :</label><input type="date" name="date_bail_rev_loca" id="date_bail_rev_loca"  required="required" /><br />

        </p>
        <p>
            <?php include_once('Form_adresse.inc.php'); ?>
        </p>
        <p>
        <h3>Contact</h3>
        <label for="num_fix_loca">Numero fix</label><input name="num_fix_loca" type="text" id="num_fix_loca" required="required" />
        <label for="num_port_loca">Numero portable :</label><input type="text" name="num_port_loca" id="num_port_loca" /><br />
        <label for="email_loca">Email :</label><input type="text" name="email_loca" id="email_loca"  />
        </p>
        <p>
        <h3>Paiement</h3>
        <?php
            $paiManager = new PaiementManager($db);
            $listePai = $paiManager->getList(); ?>
        <label for="id_pai">Mode de paiement:</label><select name="id_pai" id="id_pai" required="required">
            <option></option>
            <?php foreach($listePai as $key=>$value){
                ?><option value="<?php echo $value->getIdPai(); ?>"><?php echo $value->getLibellePai(); ?></option><?php echo "\n";
            } ?>


        </select>
        </p>

        <p><input type="reset" value="Annuler" /><input type="submit" value="Valider" /></p>
    </form>

<?php }else{
    try{
    $loca = new Locataire($_POST);

    $loca->setAdresseLoca(new Adresse($_POST));
    //var_dump($loca);
    //var_dump($_POST);
    $locaManager = new LocataireManager($db);
    LocataireRegle::dispoLoca($_POST['id_loc'],$loca,$db);
    $id_loca = $locaManager->add($loca);

    $occupeManager = new OccupeManager($db);
    $occupeManager->add(array('id_loca'=>$id_loca,'id_loc'=>$_POST['id_loc']));

    header('Location: index.php?page=114&id='.$id_loca);

    }catch (ImmolocException $e){
        ?>

        <div class="erreurSaisi"><p>
        <?php
       echo $e->getMessage();
        //header('Location: #');

        ?>
            </p>
        </div>

    <?php

    }
}
}else{
    header('Location: index.php');
}